import { defineConfig, loadEnv } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import h5ProdEffectPlugin from 'uni-vite-plugin-h5-prod-effect'
import legacy from '@vitejs/plugin-legacy'

// https://vitejs.dev/config/
export default defineConfig((mode, command) => {
  console.log(mode)
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_ENV } = env
  return {
    base: VITE_APP_ENV === 'production' ? './' : './',
    plugins: [
      uni(),
      AutoImport({
        imports: [
          'vue',
          'uni-app'
        ],
        dirs: [
          'src/utils',
          'src/wxs/**'
        ],
        dts: 'src/auto-imports.d.ts',
        eslintrc: {
          enabled: true
        }
      }),
      // 对h5 production环境打包时的特殊处理，否则uni-crazy-router在这个环境会异常
      h5ProdEffectPlugin()
    ].concat(process.env.UNI_PLATFORM === 'h5' ? [
      legacy({
        // 兼容浏览器列表
        targets: ['Android >= 7', 'chrome >= 51', 'firefox >= 54', 'ios >= 10']
      })
    ] : []),
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      }
    },
    server: {
      host: true,
      port: 80,
      open: true,
      useEslint: true,
      proxy: {
        '/dev-api': {
          target: 'https://api-test.hongkang-life.com/anyi-admin',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, '')
        },
        '/nj-api': {
          target: 'https://api-dev.idadt-tech.com',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/nj-api/, '')
        }
      }
    },
    build: {
    }
  }
})
